{% extends "base.html" %}


{% block  title %}命令行{% endblock %}

{% block page-content %}


    <div class="row wrapper border-bottom white-bg page-heading">
        <div class="col-lg-10">
            <h2>命令行</h2>
            <ol class="breadcrumb">
                <li>
                    <a href="/index.html">主页</a>
                </li>
                <li>
                    <a>任务</a>
                </li>
                <li class="active">
                    <strong>命令行</strong>
                </li>
            </ol>
        </div>
        <div class="col-lg-2">

        </div>
    </div>
    <div class="wrapper wrapper-content animated fadeInRight">
        <div class="row">
            <div class="col-lg-5">
                <div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <h5>命令行</h5>
                        <div class="ibox-tools">
                            <a class="collapse-link">
                                <i class="fa fa-chevron-up"></i>
                            </a>
                            <a class="dropdown-toggle" data-toggle="dropdown" href="#">
                                <i class="fa fa-wrench"></i>
                            </a>
                            <ul class="dropdown-menu dropdown-user">
                                <li><a href="#">选项 1</a>
                                </li>
                                <li><a href="#">选项 2</a>
                                </li>
                            </ul>
                            <a class="close-link">
                                <i class="fa fa-times"></i>
                            </a>
                        </div>
                    </div>
                    <div class="ibox-content">
                        <div class="table-responsive">
                            <form class="form-horizontal  cmd_from">
                                <table class="table table-striped table-bordered table-hover dataTables-example">
                                    <thead>
                                    <tr>
                                        <th width="20"><input type="checkbox" id="CheckedAll"></th>
                                        <th>外网IP</th>
                                        <th>管理IP</th>
                                        <th>用户</th>
                                        <th>产品线</th>

                                    </tr>
                                    </thead>
                                    <tbody>

                                    {% for   row   in   asset_list %}

                                        <tr class="gradeA" id="{{ row.id }}">
                                            <td><input type="checkbox" value="{{ row.id }}" name="id"></td>
                                            <td class="center"><a
                                                    href="{% url  "asset:asset_detail"   pk=row.id %}">{{ row.network_ip }}</a>
                                            </td>
                                            <td class="center">{{ row.manage_ip }}</td>
                                            <td class="center">{{ row.system_user }}</td>
                                            <td class="center">{{ row.product_line }} </td>
                                        </tr>
                                    {% endfor %}

                                    </tbody>

                                </table>

                                <div class="ibox-content">
                                <div class="form-group">


                                        <div>

                                            <select name="module" class="form-control" title="" required=""
                                            id="id_cmd" style="width:100px;float: left;">
                                                <option value="shell">shell</option>
                                                <option value="cron">cron</option>
                                                <option value="copy">copy</option>
                                                <option value="file">file</option>
                                                <option value="service">service</option>
                                                <option value="user">user</option>
                                                <option value="ping">ping</option>
                                                <option value="yum">yum</option>
                                                <option value="setup">setup</option>
                                                <option value="template">template</option>
                                                <option value="synchronize">synchronize</option>
                                                <option value="get_url">get_url</option>

                                            </select>
                                            &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;
                                            &nbsp;
                                            <input type="button" class="btn btn-primary  " value="增加" id="addTable"
                                                   onclick="add_tr(this)"/>
                                            <input type="button" class="btn btn-danger  " value="删除" id="deleteTable"
                                                   onclick="del_tr(this)"/>
                                            <input class="form-control" placeholder="命令"   name="args" title="" style="width:500px;">


                                        </div>


                                    </div>

                     <div class="form-group">
                                        <div class="col-sm-6 col-sm-offset-2">

                                            <a id="cmd" class="btn btn-primary" type="submit">提交</a>
                                            <button class="btn btn-white" type="button" onclick='clearBody()'>取消
                                            </button>
                                        </div>
                                    </div>



                            </div>

                                </form>
                        </div>



                    </div>
                </div>
            </div>



            <div class="col-sm-7">
                <div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <h5>输出结果
                        </h5>
                        <div class="ibox-tools">
                            <a class="collapse-links">
                                <i class="fa fa-chevron-up"></i>
                            </a>
                            <a class="dropdown-toggle" data-toggle="dropdown" href="#">
                                <i class="fa fa-wrench"></i>
                            </a>
                            <ul class="dropdown-menu dropdown-user">
                                <li><a href="#">配置选项 1</a>
                                </li>
                                <li><a href="#">配置选项 2</a>
                                </li>
                            </ul>
                            <a class="close-link">
                                <i class="fa fa-times"></i>
                            </a>
                        </div>
                    </div>
                    <div class="ibox-content  pres">

                        <pre> <span id="err"></span></pre>

                    </div>
                </div>
            </div>
        </div>
    </div>



    {% block footer-js %}
      <script type="text/JavaScript">
    $.ajaxSetup({headers: {"X-CSRFToken": '{{ csrf_token }}'}});
</script>
        <script>


            $(function () {


                $('#cmd').click(function () {



                    $.ajax({
                        url: "/tasks/cmd.html",
                        type: 'POST',
                        data: $('.cmd_from').serialize(),
                        success: function (data) {
                            var obj = JSON.parse(data);
                            cons = "";
                            if (obj.data) {
                                $.each(obj, function (data, values) {
                                    $.each(values, function (index, value) {
                                        cons += "<pre>" + "主机:" + value.ip + "\n" + "结果: \n " + value.data + "</pre>"

                                    });
                                    $(".pres").html(cons); //把内容入到这个div中即完成
                                });
                            } else {
                                $('#err').text(obj.error);
                            }

                        }

                    })
                });


            });

            function clearBody() {
                location.reload()
            }


            function add_tr(obj) {
                var tr = $(obj).parent();
                tr.after(tr.clone());
            }

            function del_tr(obj) {
                $(obj).parent().remove();
            }

        </script>


    {% endblock %}

{% endblock %}